Change ResourceEntities from SparseSet to SparseArray to speed up resource lookups#23616
Conversation
…up resource lookups.
alice-i-cecile
left a comment
There was a problem hiding this comment.
This makes sense to me, and given that perf characteristics are otherwise kind of a wash we might as well save a bit of memory.
kfc35
left a comment
There was a problem hiding this comment.
Does this need a migration guide since ResourceEntities is pub, basically noting that it no longer impl’s Deref or DerefMut?
Also, if it no longer impl’s DerefMut, should there at least be a pub method exposed to insert into ResourceEntities as well?
Beyond these minor q’s this LGTM
Co-authored-by: Kevin Chen <chen.kevin.f@gmail.com>
There isn't actually any (... Hmm, looking more closely at the one use of |
Objective
Reduce memory usage for resources, and maybe improve performance of resource lookups.
Related to #23039, but not a solution.
Solution
Change
ResourceEntitiesfrom aSparseSetto aSparseArray.SparseArrayispub (crate), so simply exposing it throughDerefwould cause privacy errors. Instead, remove theDerefimpl and add wrapper methods foriter,get, andremove. Change the return types from&EntitytoEntitynow that they aren't generic.As background: A
SparseArrayis a simpleVec<Option<V>>, while aSparseSetis aSparseArraythat maps keys to dense indexes, combined with dense arrays of keys and values. That requires a second array operation to find the actual value, but can be much better for memory usage when the values are large, since missing items only take up space for a single index instead of an entire value.But the values in
ResourceEntitiesareEntity, which are already small! ASparseArraywill always be smaller on 64-bit systems, since anEntityis the same size as ausize, and we don't need to store the additionaldenseandindicesarrays. So switching toSparseArraywill save a lookup and save memory.One drawback is that we can no longer use the dense lists to iterate all resources, so methods like
iter_resourcesnow need to scan all component ids. I don't expect this to be a problem in practice, though.iter_resourcesis rarely used, and O(components) isn't all that much worse than O(resources). If it turns out to be an issue, it's also possible to recover this data by querying theIsResourcecomponent.Testing
Inconclusive.
I attempted to run benchmarks, both
bevymarkas in the linked issue andcargo bench -p benches --bench ecs, but the results were too noisy on my machine to reach any conclusions. And now that I look more closely, we don't have many benches that even use resources!